/*! phonebar v1.0.0 | (c) created 20190327 | hejm@huayunworld.com */
define(["jquery", "phone"], function($) {
    jQuery.fn.phopanel = function(parentClass, phone, explicitNum) {
        var $parent = this;
        var phohtml = [
            '<div class="pho-panel ' + parentClass + '">',
            '<input maxlength="18" type="text" placeholder="请输入电话号码" class="pho-input">',
            '<i class="iconfont icon-bohaopanshanchu"></i>',
            '<i class="iconfont icon-bohaopan btn_callout"></i>',
            '<ul class="pho-bohaopan">',
            '<li class="pho-bohao-sub1">拨号盘<span class="iconfont icon-guanbi"></span></li>',
            '<li class="pho-bohao-sub2">',
            '<ul class="pho-bohao-btn" data-trigger="bohao">',
            '<li><a href="">1</a></li>',
            '<li><a href="">2</a></li>',
            '<li><a href="">3</a></li>',
            '<li><a href="">4</a></li>',
            '<li><a href="">5</a></li>',
            '<li><a href="">6</a></li>',
            '<li><a href="">7</a></li>',
            '<li><a href="">8</a></li>',
            '<li><a href="">9</a></li>',
            '<li><a href="">*</a></li>',
            '<li><a href="">0</a></li>',
            '<li><a href="">#</a></li>',
            '<li class="deleteBtn btn_delete"><a href="" class="iconfont icon-zuojiantou btn_delete"></a></li>',
            '<li class="callTo btn_callout"><a href="" class="iconfont icon-bohao conBtn btn_callout"></a></li>',
            '</ul>',
            '</li>',
            '</ul>',
            '</div>'
        ];
        phohtml = phohtml.join('');
        $parent.html(phohtml);
        $parent.on("keyup", ".pho-input", function(e) {
            var $input = $(this);
            var phoState = $(".pho-state>ul>li:nth-child(1)").text();
            // console.log('keyup', e, e.key)
            if (e.keyCode == 13 && $input.val().length > 0) { //判断是否为回车键 以及符合格式（正则）
                var phoNo = $input.val();
                if (parentClass === 'panel1') {
                    if (phoState && (phoState !== '暂无通话' && phoState !== 'unknown' && phoState !== '未登录')) {
                        console.log('当前非可外呼状态，无法外呼-enter', phoState);
                    } else {
                        phone.defineEvent('btn_callout', { 'callNum': phoNo, 'showNum': explicitNum })
                    }
                } else {
                    if (phoNo.indexOf('sip:') === -1) {
                        phoNo = "Tel:" + phoNo;
                    }
                    console.log('咨询外线回车-', phoNo)
                    // phone.defineEvent('btn_consult', { 'consultType': 3, 'targetNum': phoNo });
                    phone.defineEvent('btn_consult', { 'consultType': 2, 'targetNum': phoNo });
                }
            } else if ($input.val().length > 0) {
                if (parentClass === 'panel1') {
                    if (phoState && (phoState !== '暂无通话' && phoState !== 'unknown')) {
                        let numInput = Number(e.key);
                        let testResult = /^[0-9]*$/.test(numInput);
                        console.log(typeof numInput, testResult)
                        if(testResult || e.key == '#' || e.key == '*')
                        phone.ucObj.SendDTMF(e.key);
                    }
                }
            }
        });
        $parent.on("click", "a.conBtn", function(e) {
                e.stopPropagation();
                e.preventDefault();
                var $a = $(e.target);
                var phoState = $(".pho-state>ul>li:nth-child(1)").text();
                var phoNo = $a.parent().parent().parent().parent().parent().children().first().val();
                // var phoNo = $('.pho-input').val();
                console.log('拨号盘的确认按钮-----', phoNo, parentClass)
                if (parentClass === 'panel1') {
                    if (phoState && (phoState !== '暂无通话' && phoState !== 'unknown')) {
                        console.log('当前非可外呼状态，无法外呼-安拨号确认键', phoState);
                    } else {
                      phone.defineEvent('btn_callout', { 'callNum': phoNo, 'showNum': explicitNum });
                    }
                } else {
                    console.log('咨询-外线按钮----', phoNo)
                    if (phoNo.indexOf('sip:') === -1) {
                        phoNo = "Tel:" + phoNo;
                    }
                    phone.defineEvent('btn_consult', { 'consultType': 3, 'targetNum': phoNo });
                }
                // alert('我是拨号盘确认按钮');
            })
            .on("click", '.pho-panel>.icon-bohaopan', function(e) {
                e.preventDefault();
                e.stopPropagation();
                var $this = $(this);
                //拨号盘应该用组件来做 首先写功能 然后封装组件
                if ($this.next().is(":hidden")) {
                    $this.next().show();
                } else {
                    $this.next().hide();
                }
            })
            .on("click", ".pho-bohao-sub1>.icon-guanbi", function(e) {
                e.stopPropagation();
                //关闭拨号盘
                $(this).parent().parent().hide();
            })
            .on("click", '.pho-panel>.icon-bohaopanshanchu', function() {
                var $this = $(this);
                var $input = $this.prev();
                //拨号删除
                if ($input.val() !== '') {
                    $input.val('');
                    $this.hide();
                }
            })
            //拨号输入
            .on("click", "[data-trigger=bohao]>li>a", function(e) {
                e.stopPropagation();
                e.preventDefault();
                var $a = $(e.target);
                console.log('拨号盘点击的内容-----', $a.text())
                phone.ucObj.SendDTMF($a.text());
                var $input = $a.parent().parent().parent().parent().parent().children().first();
                console.log('拨号的内容数字按键', $input)
                if ($input.val().length < 12) {
                    $input.val($input.val() + $a.html());
                }
                $input.next().show();
                $input.focus();
            })
            //拨号盘删除数字
      .on("click",".pho-bohao-btn>.btn_delete>a",function(e){
        e.stopPropagation();
        e.preventDefault();
        console.log("删除btn_delete")
        $delete=$(e.target)
        var $input=$delete.parent().parent().parent().parent().parent().children().first();
        if($input.val().length>0){
          $input.val($input.val().substring(0, $input.val().length - 1))
        }
        console.log($input.val())
      })
    }
})